package com.chkj.fbook.system.controller;

import java.util.List;
import java.util.Map;

import com.chkj.fbook.common.utils.Query;
import com.chkj.fbook.common.utils.R;
import com.chkj.fbook.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chkj.fbook.common.annotation.Log;
import com.chkj.fbook.common.annotation.Permission;
import com.chkj.fbook.common.controller.BaseController;
import com.chkj.fbook.system.domain.RoleDO;

@RequestMapping("/sys/role")
@Controller
public class RoleController extends BaseController {
	
	String prefix = "system/role";
	@Autowired
	private RoleService roleService;

	@Log("角色-去到列表页面")
	@Permission(name = "sys:role:role")
	@GetMapping()
	public String role() {
		return prefix + "/role";
	}

	@Log("角色-获取列表数据")
	@Permission(name = "sys:role:role")
	@GetMapping("/list")
	@ResponseBody()
	public List<RoleDO> list(@RequestParam Map<String, Object> params) {
		Query query = new Query(params);
		List<RoleDO> roles = roleService.list(query);
		return roles;
	}

	@Log("角色-去到添加页面")
	@Permission(name = "sys:role:add")
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	@Log("角色-去到修改页面")
	@Permission(name = "sys:role:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, Model model) {
		RoleDO roleDO = roleService.get(id);
		model.addAttribute("role", roleDO);
		return prefix + "/edit";
	}

	@Log("角色-保存操作")
	@Permission(name = "sys:role:add")
	@PostMapping("/save")
	@ResponseBody()
	public R save(RoleDO role) {
		if (roleService.save(role) > 0) {
			return R.ok();
		} else {
			return R.error(1, "保存失败");
		}
	}

	@Log("角色-更新操作")
	@Permission(name = "sys:role:edit")
	@PostMapping("/update")
	@ResponseBody()
	public R update(RoleDO role) {
		if (roleService.update(role) > 0) {
			return R.ok();
		} else {
			return R.error(1, "保存失败");
		}
	}

	@Log("角色-删除操作")
	@Permission(name = "sys:role:remove")
	@PostMapping("/remove")
	@ResponseBody()
	public R save(String id) {
		if (roleService.remove(id) > 0) {
			return R.ok();
		} else {
			return R.error(1, "删除失败");
		}
	}
	
	@Log("角色-批量删除操作")
	@Permission(name = "sys:role:batchRemove")
	@PostMapping("/batchRemove")
	@ResponseBody
	public R batchRemove(@RequestParam("ids[]") String[] ids) {
		int r = roleService.batchremove(ids);
		if (r > 0) {
			return R.ok();
		}
		return R.error();
	}
}
